using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SheLife.Application.Interfaces;
using SheLife.Domain.Entities;
using System.Threading.Tasks;

namespace SheLife.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class PrivacySettingController : ControllerBase
    {
        private readonly IPrivacySettingService _privacySettingService;

        public PrivacySettingController(IPrivacySettingService privacySettingService)
        {
            _privacySettingService = privacySettingService;
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<PrivacySetting>> GetPrivacySetting(int id)
        {
            var setting = await _privacySettingService.GetPrivacySettingByIdAsync(id);
            if (setting == null)
            {
                return NotFound();
            }
            return setting;
        }

        [HttpGet("user/{userId}")]
        public async Task<ActionResult<PrivacySetting>> GetPrivacySettingByUserId(int userId)
        {
            var setting = await _privacySettingService.GetPrivacySettingByUserIdAsync(userId);
            if (setting == null)
            {
                return NotFound();
            }
            return setting;
        }

        [HttpGet("user/{userId}/datasharing")]
        public async Task<ActionResult<object>> GetDataSharingSettings(int userId)
        {
            var settings = await _privacySettingService.GetDataSharingSettingsAsync(userId);
            return Ok(settings);
        }

        [HttpGet("user/{userId}/features")]
        public async Task<ActionResult<object>> GetFeatureSettings(int userId)
        {
            var settings = await _privacySettingService.GetFeatureSettingsAsync(userId);
            return Ok(settings);
        }

        [HttpPost]
        public async Task<ActionResult<PrivacySetting>> CreatePrivacySetting(PrivacySetting setting)
        {
            var createdSetting = await _privacySettingService.CreatePrivacySettingAsync(setting);
            return CreatedAtAction(nameof(GetPrivacySetting), new { id = createdSetting.Id }, createdSetting);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdatePrivacySetting(int id, PrivacySetting setting)
        {
            if (id != setting.Id)
            {
                return BadRequest();
            }

            await _privacySettingService.UpdatePrivacySettingAsync(setting);
            return NoContent();
        }

        [HttpPut("user/{userId}/datasharing")]
        public async Task<IActionResult> UpdateDataSharingSettings(int userId, [FromBody] DataSharingSettingsRequest request)
        {
            await _privacySettingService.UpdateDataSharingSettingsAsync(
                userId, 
                request.HealthData, 
                request.LifestyleData, 
                request.SkincareData, 
                request.ProfileData);
            return NoContent();
        }

        [HttpPut("user/{userId}/features")]
        public async Task<IActionResult> UpdateFeatureSettings(int userId, [FromBody] FeatureSettingsRequest request)
        {
            await _privacySettingService.UpdateFeatureSettingsAsync(
                userId, 
                request.AIAnalysis, 
                request.DataSync, 
                request.PersonalizedRecommendations, 
                request.Notifications);
            return NoContent();
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeletePrivacySetting(int id)
        {
            await _privacySettingService.DeletePrivacySettingAsync(id);
            return NoContent();
        }
    }

    public class DataSharingSettingsRequest
    {
        public bool HealthData { get; set; }
        public bool LifestyleData { get; set; }
        public bool SkincareData { get; set; }
        public bool ProfileData { get; set; }
    }

    public class FeatureSettingsRequest
    {
        public bool AIAnalysis { get; set; }
        public bool DataSync { get; set; }
        public bool PersonalizedRecommendations { get; set; }
        public bool Notifications { get; set; }
    }
}